package es.ats.codington.festival.services;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import es.ats.codington.festival.daos.DAOManager;
import es.ats.codington.festival.domain.Event;
import es.ats.codington.festival.domain.LargeBusiness;
import es.ats.codington.festival.domain.Museum;
import es.ats.codington.festival.domain.Park;
import es.ats.codington.festival.domain.Stadium;
import es.ats.codington.festival.domain.Theater;
import es.ats.codington.festival.domain.TouristAttraction;
import es.ats.codington.festival.domain.TraditionalMarket;
import es.ats.codington.festival.domain.Zoo;
import es.ats.codington.festival.interfaces.daos.IEventDAO;
import es.ats.codington.festival.interfaces.services.IEventService;

/**
 * 
 * EventService
 *
 */
public class EventService implements IEventService {

	Connection connection = null;
	private IEventDAO eventDAO;

	/**
	 * Constructor
	 */
	
	public EventService(){
		ApplicationContext ctx = new ClassPathXmlApplicationContext(  "beans.xml");  
		DAOManager daoManager = (DAOManager) ctx.getBean("daoManager");
		this.eventDAO = daoManager.getEventDAO();
	}
	
	
	//---------------------- Methods ------------------------------
	
	/**
	 * ShowEvents
	 * 
	 * This method returns an ArrayList with all the events in the database.
	 * 
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public ArrayList<Event> getShowEvents() throws SQLException, ClassNotFoundException {
		ArrayList<Event> showEvents = new ArrayList<Event>();
		try {
			showEvents = eventDAO.showEvents();
		} catch (ClassNotFoundException exception) {
			System.out.println(exception.getMessage());
		} catch (SQLException exception) {
			System.out.println(exception.getMessage());
		}
		return showEvents;
	}
	
	/**
	 * RegisterNewEvent
	 * 
	 * This method allows the admin for registering a new event in the database using the method registerNewEvent(event) of the DAO class
	 * IS NOT IMPLEMENTED IN THIS SPRING VERSION
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int registerNewEvent(Event event) throws SQLException, ClassNotFoundException{	
		int affectedRows = 0;
		try{
			affectedRows = eventDAO.registerNewEvent(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		return affectedRows;
		}


	/**
	 * DeleteEvent
	 * 
	 *  This method allows the admin for deleting an existing event from the database using the method deleteEvent(idEvent) of the DAO class
	 * IS NOT IMPLEMENTED IN THIS SPRING VERSION
	 * 
	 * @param idEvent
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int deleteEvent(int idEvent) throws SQLException, ClassNotFoundException{
		int affectedRows = 0;
		try{
			affectedRows = eventDAO.deleteEvent(idEvent);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		return affectedRows;
	}

	/**
	 * Update Event
	 * 
	 * This method allows the admin for updating an existing event from the database using the method updateEvent(event) of the DAO class
	 * IS NOT IMPLEMENTED IN THIS SPRING VERSION
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 * @throws ClassNotFoundException
	 */
	public int updateEvent(Event event) throws SQLException, ClassNotFoundException{
		int affectedRows = 0;
		try{
			affectedRows = eventDAO.updateEvent(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		return affectedRows;
	}

	/**
	 * CheckEvent
	 * This method allows the admin for checking if an event exist in the database using the method checkEventDAO(event) of the DAO class
	 * IS NOT IMPLEMENTED IN THIS SPRING VERSION
	 * 
	 * 
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 */
	public boolean checkEvent(Event event) throws SQLException{
		boolean bool=false;
		try{
			bool=eventDAO.checkEventDAO(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		return bool;
	}
	
	/**
	 * GetIdEvent
	 * 
	 * This method returns the Id number of an existing event using the method getIdEventDAO(event) of the DAO class
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 */
	public int getIdEvent(Event event) throws SQLException{
		int idEvent=0;
		try{
			idEvent=eventDAO.getIdEventDAO(event);
		}catch(Exception e) {
			System.out.println("updateInfo: "+ e.getMessage());
		}
		return idEvent;
	}
	
	

	/**
	 * This method returns an existing event from the database using the method getEventDAO(event)
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 */
	public Event getEvent(Event e) throws SQLException{
	
	try {
		e=eventDAO.getEventDAO(e);
					
	}catch (Exception ee) {
		System.out.println("getvisitorService: "+ ee.getMessage());
	} 
	return e;
	}

	/**
	 * This method returns an ArrayList with the events of a concrete Place using the method showEventsByPlaceDAO(event)
	 * 
	 * @param event
	 * @return
	 * @throws SQLException
	 */
	public ArrayList<Event> getShowEventsByPlace(Event event) throws SQLException, ClassNotFoundException {
	ArrayList<Event> showEvents = new ArrayList<Event>();
	try {
		showEvents = eventDAO.showEventsByPlaceDAO(event);
	} catch (ClassNotFoundException exception) {
		System.out.println(exception.getMessage());
	} catch (SQLException exception) {
		System.out.println(exception.getMessage());
	}
	return showEvents;
	}



	/**
	 * This method returns the stadium attributes using the method showStadiumDAO(stadium)
	 * 
	 * @param stadium
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Stadium showStadium(Stadium stadium) throws ClassNotFoundException, SQLException {
	
	try{
		stadium= eventDAO.showStadiumDAO(stadium);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return stadium;
	}

	/**
	 * This method returns the theater attributes using the method showTheaterDAO(theater)
	 * 
	 * 
	 * @param theater
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Theater showTheater(Theater theater) throws ClassNotFoundException, SQLException {
	
	try{
		theater= eventDAO.showTheaterDAO(theater);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return theater;
	}

	/**
	 * This method returns the museum attributes using the method showmuseumDAO(museum)
	 * 
	 * @param museum
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Museum showMuseum(Museum museum) throws ClassNotFoundException, SQLException {
	
	try{
		museum= eventDAO.showMuseumDAO(museum);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return museum;
	}

	/**
	 * This method returns the largeBusiness attributes using the method showLargeBusinessDAO(largeBusiness)
	 * 
	 * @param largeBusiness
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public LargeBusiness showLargeBusiness(LargeBusiness largeBusiness) throws ClassNotFoundException, SQLException {
	
	try{
		largeBusiness= eventDAO.showLargeBusinessDAO(largeBusiness);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return largeBusiness;
	}

	/**
	 * This method returns the zoo attributes using the method showZooDAO(Zoo)
	 * 
	 * @param zoo
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Zoo showZoo(Zoo zoo) throws ClassNotFoundException, SQLException {
	
	try{
		zoo= eventDAO.showZooDAO(zoo);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return zoo;
	}

	/**
	 * 
	 * This method returns the park attributes using the method showParkDAO(park)
	 * 
	 * @param park
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public Park showPark(Park park) throws ClassNotFoundException, SQLException {
	
	try{
		park= eventDAO.showParkDAO(park);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return park;
	}
	
	/**
	 * 
	 * This method returns the traditional market attributes using the method showTraditionalMarketDAO(traditionalMarket)
	 * 
	 * @param traditionalMarket
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public TraditionalMarket showTraditionalMarket(TraditionalMarket traditionalMarket) throws ClassNotFoundException, SQLException {
	
	try{
		traditionalMarket= eventDAO.showTraditionalMarketDAO(traditionalMarket);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return traditionalMarket;
	}

	/**
	 * This method returns the touristAttraction attributes using the method showtouristAttractionDAO(touristAttraction)
	 * 
	 * @param touristAttraction
	 * @return
	 * @throws ClassNotFoundException
	 * @throws SQLException
	 */
	public TouristAttraction showTouristAttraction(TouristAttraction touristAttraction) throws ClassNotFoundException, SQLException {
	
	try{
		touristAttraction= eventDAO.showTouristAttractionDAO(touristAttraction);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return touristAttraction;
	}

	/**
	 * This method returns the id number of a concrete Place using the method getIdPlaceDAO(name) of the DAO class
	 * 
	 * @param name
	 * @return
	 * @throws SQLException
	 */
	public int getIdPlace(final String name) throws  SQLException {
	
	int id=0;
	
	try{
		id= eventDAO.getIdPlaceDAO(name);

	
	} catch(SQLException ex) {
		ex.getMessage();
	}
	return id;
	}

}